/*
 ####        # 
 #           #
 ## ### #  # #
 #  #-# #\ # #
 #  # # # \# ####
 Author: Felipe de Andrade Neves Lavratti

 Copyright: There are no restrictions. Use as you want.	  
*/

#include "lpc_config.h"

#include "uart/uart.h"
#include "mprintf/mprintf.h"
#include "timer/timer.h"
#include "rtc/rtc.h"
#include "httpd/uIP_Task.h"

/* Function prototypes */
void PLLInit( void );
void GPIOInit(void);

int main( void )
{
	int i;
	RTCTime local_time;

	PLLInit();
	GPIOInit();

    RTCInit();
    local_time.RTC_Sec = 0;
    local_time.RTC_Min = 0;
    local_time.RTC_Hour = 0;
    local_time.RTC_Mday = 8;
    local_time.RTC_Wday = 3;
    local_time.RTC_Yday = 05;
    local_time.RTC_Mon = 01;
    local_time.RTC_Year = 2011;
    RTCSetTime( local_time );
    RTCStart();

    UARTInit(115200,UART_CH_0,0);
   	UARTTransmitString("\n\nselivre.wordpress.com\nEthernet Httpd Demo!\n", UART_CH_0);

 	uart_puts("4x 1s: ");
 	delayMs(1000);
 	uart_putc('1');
 	delayMs(1000);
 	uart_putc('2');
 	delayMs(1000);
 	uart_putc('3');
 	delayMs(1000);
 	uart_putc('4');

   	uart_puts("\nTCP...");

   	uIP_Task_init();

   	uart_puts("  ok");

   	while(1)
    {uIP_Task();
  	}

	return 0;
}


void PLLInit( void )
{
	int i;
	#ifdef RUN_FROM_RAM
		/* Remap the interrupt vectors to RAM if we are are running from RAM. */
		SCB_MEMMAP = 2;
	#endif

	/* Disable the PLL. */
	PLLCON = 0;
	PLLFEED = PLL_FEED_BYTE1;
	PLLFEED = PLL_FEED_BYTE2;

	/* Configure clock source. */
	SCS |= OSC_ENABLE;
	while( !( SCS & OSC_STAT ) );
	CLKSRCSEL = OSC_SELECT;

	/* Setup the PLL to multiply the XTAL input by 8. */
	PLLCFG = ( PLL_MUL | PLL_DIV );
	PLLFEED = PLL_FEED_BYTE1;
	PLLFEED = PLL_FEED_BYTE2;

	/* Turn on and wait for the PLL to lock... */
	PLLCON = PLL_ENABLE;
	PLLFEED = PLL_FEED_BYTE1;
	PLLFEED = PLL_FEED_BYTE2;
	/* Configurate CPU clock to divide PLL by 4... */
	CCLKCFG = CPU_CLK_DIV;
	while( !( PLLSTAT & PLL_LOCK ) );

	/* Connecting the clock. */
	PLLCON = PLL_CONNECT;
	PLLFEED = PLL_FEED_BYTE1;
	PLLFEED = PLL_FEED_BYTE2;
	while( !( PLLSTAT & PLL_CONNECTED ) );

	MAMCR = 0;
	MAMTIM = 3;
	MAMCR = 2;

}
void GPIOInit(void)
{
	// Set to inputs
	IODIR0  = 0;
	IODIR1  = 0;
	FIO0DIR = 0;
	FIO1DIR = 0;
	FIO2DIR = 0;
	FIO3DIR = 0;
	FIO4DIR = 0;

	// Enable Fast GPIO0,1
	SCS|=0x01;
	// SCS_bit.GPIOM = 1;

	// clear mask registers
	FIO0MASK =0;
	FIO1MASK =0;
	FIO2MASK =0;
	FIO3MASK =0;
	FIO4MASK = 0;

	// Reset all GPIO pins to default primary function
	PINSEL0 =0;
	PINSEL1 =0;
	PINSEL2 =0;
	PINSEL3 =0;
	PINSEL4 =0;
	PINSEL5 =0;
	PINSEL6 =0;
	PINSEL7 =0;
	PINSEL8 =0;
	PINSEL9 =0;
	PINSEL10= 0;
	PINSEL11= 0;
}

void DABT_Routine()
{
	register unsigned long *r14 asm("%lr");
	unsigned long reg;
	char str[50];
	reg = (unsigned long)r14;
	sprintf(str, "\nData Abort Exception at 0x%x : OPCODE=0x%x", reg-8, *(unsigned long*)(reg-8));
	uart_puts(str);
	while(1);
}

void PABT_Routine()
{
	register unsigned long *r14 asm("%r14");
	unsigned long reg;
	char str[50];
	reg = (unsigned long)r14;
	sprintf(str, "\nPrefetch Abort Exception at 0x%x", reg-4);
	uart_puts(str);
	while(1);
}
